﻿using System;
using System.Xml;

namespace GameListEditor.HyperSpin
{
    ///-------------------------------------------------------------------------------------------------
    /// <summary> 
    /// Represents a game with properties necessary for HyperSpin compatibility. Loads and saves
    /// HyperSpin xml file format. 
    /// </summary>
    ///-------------------------------------------------------------------------------------------------
    class HyperSpinGame
    {
        #region Member Variables

        private String m_name = "";
        private String m_description = "";
        private String m_cloneOf = "";
        private String m_manufacturer = "";
        private String m_year = "";
        private String m_genre = "";

        #endregion

        #region Properties
        ///-------------------------------------------------------------------------------------------------
        /// <summary> Name of game. </summary>
        /// <value> The name. </value>
        ///-------------------------------------------------------------------------------------------------
        public String Name
        {
            get { return m_name; }
            set { if (value != null) m_name = value; else m_name = ""; }
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Game description. </summary>
        /// <value> The description. </value>
        ///-------------------------------------------------------------------------------------------------
        public String Description
        {
            get { return m_description; }
            set { if (value != null) m_description = value; else m_description = ""; }
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Parent, if game is a clone. </summary>
        /// <value> The clone of. </value>
        ///-------------------------------------------------------------------------------------------------
        public String CloneOf
        {
            get { return m_cloneOf; }
            set { if (value != null) m_cloneOf = value; else m_cloneOf = ""; }
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Game manufacturer. </summary>
        /// <value> The manufacturer. </value>
        ///-------------------------------------------------------------------------------------------------
        public String Manufacturer
        {
            get { return m_manufacturer; }
            set { if (value != null) m_manufacturer = value; else m_manufacturer = ""; }
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Year created/released. </summary>
        /// <value> The year. </value>
        ///-------------------------------------------------------------------------------------------------
        public String Year
        {
            get { return m_year; }
            set { if (value != null) m_year = value; else m_year = ""; }
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Genre/category of game. </summary>
        /// <value> The genre. </value>
        ///-------------------------------------------------------------------------------------------------
        public String Genre
        {
            get { return m_genre; }
            set { if (value != null) m_genre = value; else m_genre = ""; }
        }

        #endregion

        #region Constructors

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Constructor. </summary>
        ///-------------------------------------------------------------------------------------------------
        public HyperSpinGame()
        {
            // Auto-initialization.
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Constructor. </summary>
        /// <param name="name">         The name. </param>
        /// <param name="description">  The description. </param>
        /// <param name="cloneOf">      The clone of. </param>
        /// <param name="manufacturer"> The manufacturer. </param>
        /// <param name="year">         The year. </param>
        /// <param name="genre">        The genre. </param>
        ///-------------------------------------------------------------------------------------------------
        public HyperSpinGame(String name, String description, String cloneOf, String manufacturer, String year, String genre)
        {
            Name = name;
            Description = description;
            CloneOf = cloneOf;
            Manufacturer = manufacturer;
            Year = year;
            Genre = genre;
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Constructor. NOTE: No error checking! </summary>
        /// <param name="node"> The node to load from. </param>
        ///-------------------------------------------------------------------------------------------------
        public HyperSpinGame(XmlNode node)
        {
            Name = node.Attributes[HyperSpinXmlConst.XML_NAME].Value;
            Description = node.SelectSingleNode(HyperSpinXmlConst.XML_DESCRIPTION).InnerText;
            CloneOf = node.SelectSingleNode(HyperSpinXmlConst.XML_CLONE_OF).InnerText;
            Manufacturer = node.SelectSingleNode(HyperSpinXmlConst.XML_MANUFACTURER).InnerText;
            Year = node.SelectSingleNode(HyperSpinXmlConst.XML_YEAR).InnerText;
            Genre = node.SelectSingleNode(HyperSpinXmlConst.XML_GENRE).InnerText;
        }

        #endregion

        #region Public Methods
        ///-------------------------------------------------------------------------------------------------
        /// <summary> 
        /// Saves to specified node, which should have been already created. NOTE: No error checking! 
        /// </summary>
        /// <param name="node"> The XmlNode to save to. </param>
        ///-------------------------------------------------------------------------------------------------
        public void Save(XmlNode node)
        {
            // Save name.
            XmlAttribute nameAttribute = node.OwnerDocument.CreateAttribute(HyperSpinXmlConst.XML_NAME);
            nameAttribute.Value = Name;
            node.Attributes.Append(nameAttribute);

            // Save description.
            XmlNode descriptionNode = node.OwnerDocument.CreateElement(HyperSpinXmlConst.XML_DESCRIPTION);
            descriptionNode.InnerText = Description;
            node.AppendChild(descriptionNode);

            // Save cloneOf.
            XmlNode cloneOfNode = node.OwnerDocument.CreateElement(HyperSpinXmlConst.XML_CLONE_OF);
            cloneOfNode.InnerText = CloneOf;
            node.AppendChild(cloneOfNode);

            // Save manufacturer.
            XmlNode manufacturerNode = node.OwnerDocument.CreateElement(HyperSpinXmlConst.XML_MANUFACTURER);
            manufacturerNode.InnerText = Manufacturer;
            node.AppendChild(manufacturerNode);

            // Save year.
            XmlNode yearNode = node.OwnerDocument.CreateElement(HyperSpinXmlConst.XML_YEAR);
            yearNode.InnerText = Year;
            node.AppendChild(yearNode);

            // Save genre.
            XmlNode genreNode = node.OwnerDocument.CreateElement(HyperSpinXmlConst.XML_GENRE);
            genreNode.InnerText = Genre;
            node.AppendChild(genreNode);
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary> Return game properties as an array. </summary>
        /// <returns> Array of properties. </returns>
        ///-------------------------------------------------------------------------------------------------
        public String[] AsArray()
        {
            string[] propertiesAsArray = new string[6];

            propertiesAsArray[0] = Description;
            propertiesAsArray[1] = Name; // Note order of description and name is reversed.
            propertiesAsArray[2] = CloneOf;
            propertiesAsArray[3] = Manufacturer;
            propertiesAsArray[4] = Year;
            propertiesAsArray[5] = Genre;

            return (propertiesAsArray);
        }

        #endregion
    }
}
